package cn.zspt.vank.controller;

import cn.zspt.vank.domain.*;
import cn.zspt.vank.domain.dto.OrderQuery;
import cn.zspt.vank.service.OrderService;
import cn.zspt.vank.util.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "点餐系统api",description = "点餐系统功能api")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/category/{categoryId}/dishes")
    @Operation(summary = "获取指定分类下菜品")
    public List<Dish> findDishByCategory(@PathVariable int categoryId){
        return orderService.findDishByCategory(categoryId);
    }

    @GetMapping("/category")
    @Operation(summary = "获取所有分类")
    public List<Category> findAllCategory(){
        return orderService.findAllCategory();
    }

    @PostMapping("/order")
    @Operation(summary = "生成订单",description = "生成订单，同时生成该订单下所有订单明细")
    public String createOrder(
            @Parameter(description = "订单数据，包含该订单下所有订单明细", required = true)
            @RequestBody Order order,
            @RequestHeader("Authorization") String token){
        int userId=JwtUtils.getUid(token);
        order.getOperator().setUserId(userId);
        orderService.createOrder(order);
        return "订单创建成功";
    }

    @GetMapping("/order/today")
    @Operation(summary = "获取指定操作人当日订单")
    public List<Order> listOrderToday(@RequestHeader("Authorization") String token){
        int userId=JwtUtils.getUid(token);
        return orderService.findOrderTodayByUser(userId);
    }

    @GetMapping("/user")
    @Operation(summary = "获取所有用户信息",description = "不含密码，权限")
    public List<User> findAllUser(){
        return orderService.findAllUser();
    }

    @GetMapping("/order/orderItem/{orderId}")
    @Operation(summary = "根据订单id查询订单明细")
    public List<OrderItem> findOrderItems(@PathVariable int orderId){
        return orderService.findOrderItems(orderId);
    }

    @PostMapping("/order/query")
    @Operation(summary = "根据条件查询订单")
    public List<Order> queryOrder(@RequestBody OrderQuery orderQuery){
        return orderService.findOrderByQuery(orderQuery);
    }

    @PutMapping("/order/take/{orderId}")
    @Operation(summary = "确认取餐",description = "修改订单状态到确认取餐")
    public String takeOrder(@PathVariable int orderId){
        orderService.takeOrder(orderId);
        return "确认取餐";
    }

    @PutMapping("/order/pay/{orderId}")
    @Operation(summary = "完成订单",description = "修改订单状态到完成订单")
    public String payOrder(@PathVariable int orderId){
        orderService.payOrder(orderId);
        return "完成订单";
    }

    @PutMapping("/order/abort/{orderId}")
    @Operation(summary = "退回订单",description = "修改订单状态到退回订单")
    public String abortOrder(@PathVariable int orderId){
        orderService.abortOrder(orderId);
        return "退回订单";
    }
}
